#include "bsp_can.h"
#include "can.h"
#include "myconfig.h"
#include "ABS_key.h"
#include "display.h"

uint8_t RxID;
uint8_t RxLength;
uint8_t Data[8];
uint8_t RxFlag = 0;

void CAN_Init(void)
{
    HAL_CAN_Start(&hcan1);
    HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
    HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO1_MSG_PENDING);
}

void CAN_FliterInit(uint32_t ID,uint32_t Mask,uint32_t FilterBank)
{
    CAN_FilterTypeDef CAN_FilterStructure;
    CAN_FilterStructure.FilterMode = CAN_FILTERMODE_IDMASK;
    CAN_FilterStructure.FilterIdHigh = 0x0000;
    CAN_FilterStructure.FilterIdLow = 0x0000;
    CAN_FilterStructure.FilterMaskIdHigh = 0x0000;
    CAN_FilterStructure.FilterMaskIdLow = 0x0000;
    CAN_FilterStructure.FilterBank = FilterBank;
    CAN_FilterStructure.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    CAN_FilterStructure.FilterScale = CAN_FILTERSCALE_16BIT;
    CAN_FilterStructure.SlaveStartFilterBank = 14;
    CAN_FilterStructure.FilterActivation = CAN_FILTER_ENABLE;
    HAL_CAN_ConfigFilter(&hcan1,&CAN_FilterStructure);
}

void CAN_Transmit(uint32_t ID,uint8_t length,uint8_t Data[])
{
    uint32_t TxMail;
    CAN_TxHeaderTypeDef CAN_TxHeaderStructure;
    CAN_TxHeaderStructure.StdId = ID;
    CAN_TxHeaderStructure.ExtId = ID;
    CAN_TxHeaderStructure.DLC = length;
    CAN_TxHeaderStructure.IDE = CAN_ID_STD;
    CAN_TxHeaderStructure.RTR = CAN_RTR_DATA;
    CAN_TxHeaderStructure.TransmitGlobalTime = DISABLE;
    HAL_CAN_AddTxMessage(&hcan1,&CAN_TxHeaderStructure,Data,&TxMail);
}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    RxFlag = 1;
    while (HAL_CAN_GetRxFifoFillLevel(hcan,CAN_RX_FIFO0))
    {
        CAN_RxHeaderTypeDef CAN_RxHeaderStruct;
        HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&CAN_RxHeaderStruct,Data);
        if(CAN_RxHeaderStruct.RTR == CAN_RTR_DATA)
        {
            if(CAN_RxHeaderStruct.IDE == CAN_ID_STD)
                RxID = CAN_RxHeaderStruct.StdId;
            else
                RxID = CAN_RxHeaderStruct.ExtId;
            RxLength = CAN_RxHeaderStruct.DLC;
        }
        else
        {

        }
    }
    
}

My_StatusTypeDef CAN_Test(int key_Num)
{
    static uint8_t num[4] = {0x11,0x22,0x33,0x44};
    static uint8_t length = sizeof(num)/sizeof(uint8_t);
    // displayNum(RxFlag,Display_firstCol,Display_fifthRow);
    if(RxFlag)
    {
        RxFlag = 0;
        displayStr("RxID:",Display_firstCol,Display_firstRow,RED);
        displayNum(RxID,Display_firstCol+Display_OneChar*5,Display_firstRow);
        displayStr("Data:",Display_firstCol,Display_secondRow,RED);
        displayNum(Data[0],Display_firstCol,Display_thirdRow);
        displayNum(Data[1],Display_firstCol,Display_fourthRow);
        displayNum(Data[2],Display_firstCol,Display_fifthRow);
        displayNum(Data[3],Display_firstCol,Display_fifthRow+Display_secondRow);
    }
    if (key_Num == Key_ESC) {
        return MY_OK;
    } else if (key_Num == Key_OK) {
        return MY_STANDBY;
    } else if (key_Num == Key_Down) {
        num[0]--;
        num[1]--;
        num[2]--;
        num[3]--;
        CAN_Transmit(0x111,length,num);
    } else if (key_Num == Key_UP) {
        num[0]++;
        num[1]++;
        num[2]++;
        num[3]++;
        CAN_Transmit(0x111,length,num);
    }

    return MY_BUSY;
}


